#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qfiledialog.h>
#include <qlabel.h>
#include <QMdiSubWindow>
#include <QScrollArea>
#include <QPushButton>
#include "xianshi.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    /*添加状态栏*/
        QStatusBar *pstatusBar = this->statusBar();

        pstatusBar->addWidget(status_label_1 = new QLabel("OK"));
        status_label_1->setText("<font color = red>Processing...</font>");

        pstatusBar->addWidget(status_label_2 = new QLabel("字数"));
        pstatusBar->addWidget(status_label_3 = new QLabel("字宽"));
        pstatusBar->addWidget(status_label_4 = new QLabel("字高"));




    myGridLayout=new QGridLayout();
    myGridLayout->setSizeConstraint(QGridLayout::SetMinAndMaxSize);
    myGridLayout->setMargin(10);


    ui->scrollAreaWidgetContents->setLayout(myGridLayout);

    //菜单
    ui->action_isBox->setChecked(true);
    ui->action_isZI->setChecked(true);


}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_action_exit_triggered()
{
    clear_show();
    myrecognizer.clear();
    this->close();
}
void MainWindow::clear_show()
{
    char str[256];
    while(1){
        int num=myGridLayout->count();
        if(!num) break;
        QWidget *p=myGridLayout->itemAt(num-1)->widget();
        myGridLayout->removeWidget(p);
        delete p;
        sprintf(str,"共%d字",num);
        status_label_2->setText(str);

    }
    sprintf(str,"共%d字",0);
    status_label_2->setText(str);
    status_label_3->setText("宽");
    status_label_4->setText("高");
    status_label_1->setText("已关闭！");
    this->update();
}

void MainWindow::show_hanzi()
{

    int i = 0;
        han_zi:: hanzi* my_zi;
        int c=9;
         char str[256];
         for (i=0; i<myrecognizer.characters.size(); ++i)
        {

            HandWriteRecognizer::Character* mycharacter=&(myrecognizer.characters[i]);
           my_zi=new han_zi::hanzi(NULL,mycharacter);
           sprintf(str,"myzi_%d",i);
           my_zi->setObjectName(str);
            my_zi->setMinimumSize(100,100);
            my_zi->setMaximumSize(100,100);
            my_zi->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
            my_zi->isBox=true;
            my_zi->isDispStr=true;
            myGridLayout->addWidget(my_zi,i/c,i%c,1,1);
            myGridLayout->setHorizontalSpacing(0);
            sprintf(str,"共%d字",i+1);
            status_label_2->setText(str);            
            sprintf(str,"宽：%d",mycharacter->width);
            status_label_3->setText(str);
            sprintf(str,"高：%d",mycharacter->height);
            status_label_4->setText(str);

        }
         status_label_1->setText("显示完成！");

    this->update();
}

bool MainWindow::open_file(QString file)
{
    bool b=false;
    if(file.length()>0){
    QByteArray ba = file.toLatin1();
        char *mm = ba.data();
        clear_show();
        myrecognizer.clear();
    if( myrecognizer.openFile(mm)){
        status_label_1->setText("打开成功！");
        
        b=true;
    }
    else
        status_label_1->setText("<font color = red>打开失败...</font>");
    }
    return b;
}

void MainWindow::on_action_openfile_triggered()
{
    QString strFile = QFileDialog::getOpenFileName();
   if(open_file(strFile)){

    show_hanzi();
   }
}

void MainWindow::on_action_save_triggered()
{
    QFileDialog::getSaveFileName();
}

void MainWindow::on_action_model_user_triggered()
{

    char modefilepath[256];
   sprintf(modefilepath,"%s/handwrite.model",getenv("HOME"));
   if(open_file(modefilepath)){

    show_hanzi();
   }


}

void MainWindow::on_action_model_default_triggered()
{

   if(open_file("/usr/share/shuoxie/model/handwrite.model")){

    show_hanzi();
   }

}

void MainWindow::on_action_close_triggered()
{
    clear_show();
    myrecognizer.clear();
}

void MainWindow::on_action_isBox_triggered(bool checked)
{
    //printf("%d\n",checked);
    for(int num=0;num<myGridLayout->count();num++){

        han_zi:: hanzi* p=(han_zi:: hanzi*)myGridLayout->itemAt(num)->widget();
        p->isBox=checked;
    }
}

void MainWindow::on_action_isZI_triggered(bool checked)
{
    for(int num=0;num<myGridLayout->count();num++){

        han_zi:: hanzi* p=(han_zi:: hanzi*)myGridLayout->itemAt(num)->widget();
        p->isDispStr=checked;
    }
}
